<?php

function organizations_handle_edit_page($guid, $section = '')
{
	elgg_set_page_owner_guid($guid);
	set_input('o',$guid);
	$org = get_entity($guid);
	
	elgg_push_breadcrumb($org->title,$org->getUrl());
	elgg_push_breadcrumb(elgg_echo("organizations:edit_org"));
		
	$content = elgg_view('frontend/edit/body', array('entity' => $org, 'section' => $section));
	$sidebar = elgg_view('frontend/sidebar/organization_profile', array('entity' => $org));
	
	$params = array(
		'content' => $content,
		'sidebar' => $sidebar,
		'title' => "",
		'filter' => '',
	);
	$body = elgg_view_layout('one_sidebar', $params);
	echo elgg_view_page($org->title, $body);
}

function organizations_handle_profile_page($guid, $section = '')
{
	elgg_set_page_owner_guid($guid);
	$org = get_entity($guid);
	
	elgg_push_breadcrumb($org->title);
	organizations_register_profile_buttons($org);
	$content = elgg_view('frontend/profile/body', array('section' => $section, 'entity' => $org));
	$sidebar = elgg_view('frontend/sidebar/organization_profile', array('entity' => $org));
	$params = array
	(
		'title' 	=> $org->title,
		'content' 	=> $content,
		'filter' 	=> '',
		'sidebar' 	=> $sidebar,
	);
	$body = elgg_view_layout('content', $params);
	echo elgg_view_page($org->name, $body);
}

function organizations_register_profile_buttons($org)
{
	$user = get_loggedin_user();
	if(get_context() == 'admin' || $user->isAdmin() || $org->isManager())
	{
		elgg_register_menu_item('title', array(
					'name' => "edit_org",
					'href' => "/organizations/edit/".$org->guid,
					'text' => elgg_echo("organizations:edit_org"),
					'link_class' => 'elgg-button elgg-button-action',));
		
		elgg_register_menu_item('title', array(
					'name' => "create_tournament",
					'href' => "tournaments/add/".$org->guid,
					'text' => elgg_echo("organizations:create_tournament"),
					'link_class' => 'elgg-button elgg-button-action',));
	}
}


/**
 * Check user is an organization admin
 * @deprecated Use Organization::isManager($user)
 */
function organizations_is_admin($org, $user = 0)
{
	if(!$user) $user = get_loggedin_user();
	if($user->isAdmin()) return true;
	
	$entities = $org->getEntitiesFromRelationship('org_admin');
	foreach($entities as $e)
	{
		if($e->guid == $user->guid)
			return true;
	}
	
	return false;
}
?>